package pyrachnid.ctrl.listener;

import pyrachnid.ctrl.Observer;
import pyrachnid.ctrl.Simulator;
import pyrachnid.model.ChangeValueFire;
import pyrachnid.model.ground_model.Cell;
import pyrachnid.view.MainWindow;

public class listenerFire extends Observer {
	public listenerFire(Simulator p_sim, MainWindow p_view) {
		super(p_sim, p_view);
	}

	@Override
	public void update(Object p_o, String p_arg) {
		Cell cellFire = null;
		
		// Search the cell in fire. The cell is more important that the fire in the Observer.
		for(int i = 0; i < _simulator.getSenario().getMap().getFires().size() && cellFire == null; i++) {
			Cell cellTmp = _simulator.getSenario().getMap().getFires().get(i);
			
			if(cellTmp.getFire().equals(p_o))
				cellFire = cellTmp;
		}
		
		if(p_arg.equals("FireIntensity"))
		{
			if(cellFire.getFire().getFireIntensity() > 0)
			{
				//System.out.println("INFO : Fire in : x=" + cellFire.getPositionX() + " y=" + cellFire.getPositionY() + " change value " + cellFire.getFire().getFireIntensity() + " in " + this.getClass().getName());
				cellFire.addOperation(new ChangeValueFire(cellFire,cellFire.getFire().getFireIntensity()));
			}
			else
			{
				cellFire.getFire().detach(this);
				cellFire.setIsAllocated(false);
				cellFire.setFire(null);
				_view.getPanneau().removeFireImage(cellFire);
			}
			
			
		}
	}
}